package com.hongyuan.sellfitness.ui.main.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.alipay.sdk.app.PayTask;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.AppViewModelFactory;
import com.hongyuan.sellfitness.base.CustomActivity;
import com.hongyuan.sellfitness.databinding.ActivityPayBinding;
import com.hongyuan.sellfitness.ui.main.beans.AlipayBean;
import com.hongyuan.sellfitness.ui.main.beans.V3SuccessBeans;
import com.hongyuan.sellfitness.ui.main.view_mode.PayViewModel;
import com.hongyuan.sellfitness.wxapi.PayUtil;
import com.hongyuan.sellfitness.wxapi.WXPayEntryActivity;
import com.hongyuan.sellfitness.wxapi.WecathPayBean;

import java.util.Map;

import me.tatarka.bindingcollectionadapter2.BR;

public class PayActivity extends CustomActivity<ActivityPayBinding, PayViewModel> {

    private V3SuccessBeans successBeans;

    //支付成功
    private final int PAY_SUCCESS = 0x1;
    //支付成功
    private final int PAY_FAILURE = 0x2;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_pay;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public PayViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(PayViewModel.class);
    }

    @Override
    public void initParam() {
        super.initParam();

        successBeans = (V3SuccessBeans) getBundle().getSerializable("successBeans");
    }

    @Override
    public void initView() {
        super.initView();
        setOnRefresh(binding.refresh,REFRESH_0X4);

        binding.comBack.setOnClickListener(view -> finish());

        binding.showPrice.setText(getBundle().getString("payPrice"));

    }

    @Override
    public void initData() {
        super.initData();

        viewModel.clearParams().setParams("o_id",getBundle().getString("payOid"));
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.changPayType.observe(this, s -> {
            if("wechatPay".equals(s)){
                binding.wechatPay.setImageResource(R.mipmap.yuanquanquan_select_img);
                binding.alipayPay.setImageResource(R.mipmap.yuanquanquan_no_select_img);
            }else if("alipayPay".equals(s)){
                binding.wechatPay.setImageResource(R.mipmap.yuanquanquan_no_select_img);
                binding.alipayPay.setImageResource(R.mipmap.yuanquanquan_select_img);
            }
        });

        viewModel.uc.openWX.observe(this, wecathPayBean -> {
            //微信跳转要设置下
            WXPayEntryActivity.successBeans = successBeans;
            //去掉起微信支付
            PayUtil.WechatPay(this,wecathPayBean);
        });

        viewModel.uc.openAlipay.observe(this, alipayBean -> {
            Runnable payRunnable = () -> {
                PayTask alipay = new PayTask(this);
                Map<String,String> result = alipay.payV2(alipayBean.getPay(),true);
                Message msg = new Message();
                if(result.get("resultStatus").equals("9000")){
                    msg.what = PAY_SUCCESS;
                }else{
                    msg.what = PAY_FAILURE;
                }
                mHandler.sendMessage(msg);
            };
            // 必须异步调用
            Thread payThread = new Thread(payRunnable);
            payThread.start();
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case PAY_SUCCESS:
                    viewModel.goSuccess(successBeans);
                    break;
                case PAY_FAILURE:
                    viewModel.goNext(successBeans);
                    break;
            }
        }
    };
}
